package deep.projects.newsapp.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wuxinwudai.android.app.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import deep.projects.newsapp.R;
import deep.projects.newsapp.adapters.NewsFragmentPagerAdapter;
import deep.projects.newsapp.app.NewsApplication;
import deep.projects.newsapp.beans.NewsType;
import deep.projects.newsapp.beans.User;
import deep.projects.newsapp.fragments.NewsPageFragment;
import deep.projects.newsapp.utils.TimeSquareDialogUtils;
import deep.projects.newsapp.utils.ViewUtils;

public class MainActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.navigation_view)
    NavigationView mNavigationView;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;//
    private LinearLayout mNavigationViewHead;
    private LinearLayout mllIsLogin;//已经登录
    private LinearLayout mllNotLogin;//尚未登录
    private ImageView mivAvatar;//用户头像
    private ImageView mivUserLevel;//用户等级
    private TextView mtvUserLevel;
    private User mUser;//当前用户
    private NewsFragmentPagerAdapter mnfpa;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mNavigationViewHead = (LinearLayout) getLayoutInflater().inflate(R.layout.navigation_view_head, null);
        mNavigationView.addHeaderView(mNavigationViewHead);//设置NavigationView的头部
        findViewFromNaviagtionViewHead();
        mUser = NewsApplication.getUser();
        setView();
    }

    //初始化 NavigationView 子控件
    private void findViewFromNaviagtionViewHead(){
        mtvUserLevel = (TextView) mNavigationViewHead.findViewById(R.id.tv_user_level);
        mivUserLevel = (ImageView) mNavigationViewHead.findViewById(R.id.iv_user_level);
        mllIsLogin = (LinearLayout)mNavigationViewHead.findViewById(R.id.ll_is_login);
        mllNotLogin = (LinearLayout) mNavigationViewHead.findViewById(R.id.ll_not_login);
        mivAvatar = (ImageView) mNavigationViewHead.findViewById(R.id.iv_avatar);
    }
    //设置所有 View
    private void setView(){
        mToolbar.setTitle(R.string.newsapp);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.mipmap.ic_notification_content);
        actionBar.setDisplayHomeAsUpEnabled(true);
        ArrayList<NewsPageFragment> fragments = new ArrayList<>();
        for (NewsType type : NewsType.getNewsTypes()){
            fragments.add(NewsPageFragment.create(type));
        }
        mnfpa = new NewsFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        mViewPager.setAdapter(mnfpa);
        mTabLayout.setupWithViewPager(mViewPager);
        setNavigationViewMenu();
        setNavigationViewrHead();
    }
    //设置 NavigationView Menu
    private void setNavigationViewMenu(){
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.menu_share:{
                        //分享APP
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share));
                        intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_content)+ "");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(Intent.createChooser(intent, getString(R.string.share)));
                        break;
                    }
                    case R.id.menu_feedback:{
                        ViewUtils.goFeedbackActivity(MainActivity.this);
                        break;
                    }
                    case R.id.menu_settings:{
                        ViewUtils.goSettingActivity(MainActivity.this);
                        break;
                    }
                    case R.id.menu_test:{

                        break;
                    }
                }
                mDrawerLayout.closeDrawers();
                return false;
            }
        });
    }
    //设置 NavigationView HeadLayout
    private void setNavigationViewrHead(){
        if(mUser == null){
            mllIsLogin.setVisibility(View.GONE);
            mllNotLogin.setVisibility(View.VISIBLE);
            mivAvatar.setImageResource(R.mipmap.ic_avatar_default);
        }else {
            mllIsLogin.setVisibility(View.VISIBLE);
            mllNotLogin.setVisibility(View.GONE);
//            ImageLoader.ImageListener listener  = ImageLoader.getImageListener(mivAvatar,R.mipmap.ic_avatar_default,R.mipmap.ic_advice);
//            mImageLoader.get(mUser.getAvatar(), listener);listener
//            mivUserLevel.setImageBitmap(ImageUtils.getBitmap(this, "level/ic_level_" + String.valueOf(mUser.getLevel()) + ".png"));
//            mtvUserLevel.setText(String.valueOf(mUser.getLevel()) + "级");
        }
        mivUserLevel.setOnClickListener(this);
        mllIsLogin.setOnClickListener(this);
        mllNotLogin.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                //主界面左上角的icon点击反应
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_avatar:{
                if (mUser==null){
                    ViewUtils.goLoginActivity(MainActivity.this);
                }else {
                    ViewUtils.goProfileActivity(MainActivity.this);
                }
                break;
            }
            case R.id.ll_not_login:{
                ViewUtils.goLoginActivity(MainActivity.this);
                break;
            }
            case R.id.ll_is_login:{
                ViewUtils.goProfileActivity(MainActivity.this);
                break;
            }
        }
    }
}
